<template>
  <div class="account-box">
    <div class="form">
      <Form
        class="form"
        :validation-schema="mySchema"
        v-slot="{ errors }"
        autocomplete="off"
        ref="target"
      >
        <!-- 表单元素 -->
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <!-- 当校验不通过的时候 errors.account存放了当前需要提示用户的错误信息 -->
            <!-- 一旦 errors.account有值 error类名存在 就会显示一个红色的警示框 -->
            <Field
              v-model="form.account"
              name="account"
              type="text"
              placeholder="请输入用户名或手机号"
              :class="{ error: errors.account }"
            />
            <!-- <div class="error"><i class="iconfont icon-warning" />请输入手机号</div> -->
          </div>
          <!-- 表单验证错误信息提示 -->
          <div class="error" v-if="errors.account">
            <i class="iconfont icon-warning" />{{ errors.account }}
          </div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field
              :class="{ error: errors.password }"
              v-model="form.password"
              name="password"
              type="text"
              placeholder="请输入密码"
            />
            <!-- <input type="password" placeholder="请输入密码" /> -->
          </div>
          <div class="error" v-if="errors.password">
            <i class="iconfont icon-warning" />{{ errors.password }}
          </div>
        </div>
        <div class="form-item">
          <div class="agree">
            <Field as="XtxCheckbox" name="isAgree" v-model="form.isAgree" />
            <!-- <XtxCheckBox /> -->
            <span>我已同意</span>
            <a href="javascript:;">《隐私条款》</a>
            <span>和</span>
            <a href="javascript:;">《服务条款》</a>
          </div>
          <!-- 表单验证错误信息提示 -->
          <div class="error" v-if="errors.isAgree">
            <i class="iconfont icon-warning" />{{ errors.isAgree }}
          </div>
        </div>
        <a href="javascript:;" @click="submit" class="btn">登录</a>
      </Form>
    </div>
    <div class="action">
      <img
        src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
        alt=""
      />
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>
<script>
// Form 外层的表单组件
// Field 内层的具体的表单元素组件
import { Form, Field } from 'vee-validate'
// 引入我们定义好的校验函数
import schema from '@/utils/vee-validate-schema'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import message from '@/components/Message'

export default {
  components: {
    Form,
    Field
  },
  setup () {
    // 表单对象数据
    const form = reactive({
      account: '', // 账号
      password: '', // 密码
      isAgree: true // 是否同意协议
    })
    // 校验规则对象
    const mySchema = {
      account: schema.account,
      password: schema.password,
      isAgree: schema.isAgree
    }
    // 兜底校验
    const target = ref(null)
    const router = useRouter()
    const store = useStore()
    async function submit () {
      // 实现兜底校验
      const valid = await target.value.validate()
      // console.log(valid)
      // console.log('12312313', target.value)
      // 如果valid为true 就执行登录逻辑 todo
      if (valid) {
        // 调用vuex的异步操作,
        store.dispatch('user/asyncSetUser', form).then(() => {
          // 提示用户登录成功
          message({ type: 'success', text: '登录成功' })
          // 跳转到首页
          router.push('/')
        })
      }
    }
    return {
      form,
      mySchema,
      submit,
      target
    }
  }
}
</script>

<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 20px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    // .btn {
    //   display: block;
    //   width: 100%;
    //   height: 40px;
    //   color: #fff;
    //   text-align: center;
    //   line-height: 40px;
    //   background: @xtxColor;
    //   &.disabled {
    //     background: #cfcdcd;
    //   }
    // }

    .btn {
      // position: absolute;
      // top: 50%;
      // left: 50%;
      // transform: translate(-50%, -50%);
      display: block;
      width: 100%;
      height: 40px;
      line-height: 40px;
      text-align: center;
      color: #fff;
      font-size: 25px;
      text-transform: uppercase;
      cursor: pointer;
      background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
      background-size: 400%;
      border-radius: 60px;
    }

    .btn:hover {
      animation: animate 1s linear infinite;
    }

    @keyframes animate {
      0% {
        background-position: 0%;
      }

      100% {
        background-position: 400%;
      }
    }

    .btn::before {
      content: "";
      // position: absolute;
      // top: -5px;
      // left: -5px;
      // right: -5px;
      // bottom: -5px;
      z-index: -1;
      background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
      background-size: 400%;
      border-radius: 40px;
      opacity: 0;
      transition: 0.5s;
    }

    .btn:hover::before {
      filter: blur(20px);
      opacity: 1;
      animation: animate 1s linear infinite;
    }

    //************************************ */
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
